const app = getApp<IAppOption>() as any
const commonUrl = app.globalData.commonUrl + '/'
let header = {
  'content-type': 'application/json',
} as any

export const requestGET = (url: string, parmas: any) => {
  if (wx.getStorageSync('token')) {
    header.Authorization = 'Bearer ' + wx.getStorageSync('token')
  }
  return new Promise((resolve, reject) => {
    wx.request({
      method: 'GET',
      url: commonUrl + url,
      data: parmas,
      header: header,
      success: (res: any) => {
        // 请求成功，就将成功的数据返回出去
        resolve(res)
      },
      fail: (err) => {
        reject(err)
      },
    })
  })
}

export const requestPOST = (url: string, parmas: any) => {
  if (wx.getStorageSync('token')) {
    header.Authorization = 'Bearer ' + wx.getStorageSync('token')
  }
  return new Promise((resolve, reject) => {
    wx.request({
      method: 'POST',
      url: commonUrl + url,
      data: parmas,
      header: header,
      success: (res: any) => {
        // 请求成功，就将成功的数据返回出去
        resolve(res)
      },
      fail: (err) => {
        reject(err)
      },
    })
  })
}

export const requestPUT = (url: string, parmas: any) => {
  if (wx.getStorageSync('token')) {
    header.Authorization = 'Bearer ' + wx.getStorageSync('token')
  }
  return new Promise((resolve, reject) => {
    wx.request({
      method: 'PUT',
      url: commonUrl + url,
      data: parmas,
      header: header,
      success: (res: any) => {
        // 请求成功，就将成功的数据返回出去
        resolve(res)
      },
      fail: (err) => {
        reject(err)
      },
    })
  })
}